<!DOCTYPE html><html><head>
      <title>l1</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      
      <link rel="stylesheet" href="file:///c:\Users\think\.vscode\extensions\shd101wyy.markdown-preview-enhanced-0.8.13\crossnote\dependencies\katex\katex.min.css">
      
      
      
      
      
      <style>
      code[class*=language-],pre[class*=language-]{color:#333;background:0 0;font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.4;-moz-tab-size:8;-o-tab-size:8;tab-size:8;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none}pre[class*=language-]{padding:.8em;overflow:auto;border-radius:3px;background:#f5f5f5}:not(pre)>code[class*=language-]{padding:.1em;border-radius:.3em;white-space:normal;background:#f5f5f5}.token.blockquote,.token.comment{color:#969896}.token.cdata{color:#183691}.token.doctype,.token.macro.property,.token.punctuation,.token.variable{color:#333}.token.builtin,.token.important,.token.keyword,.token.operator,.token.rule{color:#a71d5d}.token.attr-value,.token.regex,.token.string,.token.url{color:#183691}.token.atrule,.token.boolean,.token.code,.token.command,.token.constant,.token.entity,.token.number,.token.property,.token.symbol{color:#0086b3}.token.prolog,.token.selector,.token.tag{color:#63a35c}.token.attr-name,.token.class,.token.class-name,.token.function,.token.id,.token.namespace,.token.pseudo-class,.token.pseudo-element,.token.url-reference .token.variable{color:#795da3}.token.entity{cursor:help}.token.title,.token.title .token.punctuation{font-weight:700;color:#1d3e81}.token.list{color:#ed6a43}.token.inserted{background-color:#eaffea;color:#55a532}.token.deleted{background-color:#ffecec;color:#bd2c00}.token.bold{font-weight:700}.token.italic{font-style:italic}.language-json .token.property{color:#183691}.language-markup .token.tag .token.punctuation{color:#333}.language-css .token.function,code.language-css{color:#0086b3}.language-yaml .token.atrule{color:#63a35c}code.language-yaml{color:#183691}.language-ruby .token.function{color:#333}.language-markdown .token.url{color:#795da3}.language-makefile .token.symbol{color:#795da3}.language-makefile .token.variable{color:#183691}.language-makefile .token.builtin{color:#0086b3}.language-bash .token.keyword{color:#0086b3}pre[data-line]{position:relative;padding:1em 0 1em 3em}pre[data-line] .line-highlight-wrapper{position:absolute;top:0;left:0;background-color:transparent;display:block;width:100%}pre[data-line] .line-highlight{position:absolute;left:0;right:0;padding:inherit 0;margin-top:1em;background:hsla(24,20%,50%,.08);background:linear-gradient(to right,hsla(24,20%,50%,.1) 70%,hsla(24,20%,50%,0));pointer-events:none;line-height:inherit;white-space:pre}pre[data-line] .line-highlight:before,pre[data-line] .line-highlight[data-end]:after{content:attr(data-start);position:absolute;top:.4em;left:.6em;min-width:1em;padding:0 .5em;background-color:hsla(24,20%,50%,.4);color:#f4f1ef;font:bold 65%/1.5 sans-serif;text-align:center;vertical-align:.3em;border-radius:999px;text-shadow:none;box-shadow:0 1px #fff}pre[data-line] .line-highlight[data-end]:after{content:attr(data-end);top:auto;bottom:.4em}html body{font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif;font-size:16px;line-height:1.6;color:#333;background-color:#fff;overflow:initial;box-sizing:border-box;word-wrap:break-word}html body>:first-child{margin-top:0}html body h1,html body h2,html body h3,html body h4,html body h5,html body h6{line-height:1.2;margin-top:1em;margin-bottom:16px;color:#000}html body h1{font-size:2.25em;font-weight:300;padding-bottom:.3em}html body h2{font-size:1.75em;font-weight:400;padding-bottom:.3em}html body h3{font-size:1.5em;font-weight:500}html body h4{font-size:1.25em;font-weight:600}html body h5{font-size:1.1em;font-weight:600}html body h6{font-size:1em;font-weight:600}html body h1,html body h2,html body h3,html body h4,html body h5{font-weight:600}html body h5{font-size:1em}html body h6{color:#5c5c5c}html body strong{color:#000}html body del{color:#5c5c5c}html body a:not([href]){color:inherit;text-decoration:none}html body a{color:#08c;text-decoration:none}html body a:hover{color:#00a3f5;text-decoration:none}html body img{max-width:100%}html body>p{margin-top:0;margin-bottom:16px;word-wrap:break-word}html body>ol,html body>ul{margin-bottom:16px}html body ol,html body ul{padding-left:2em}html body ol.no-list,html body ul.no-list{padding:0;list-style-type:none}html body ol ol,html body ol ul,html body ul ol,html body ul ul{margin-top:0;margin-bottom:0}html body li{margin-bottom:0}html body li.task-list-item{list-style:none}html body li>p{margin-top:0;margin-bottom:0}html body .task-list-item-checkbox{margin:0 .2em .25em -1.8em;vertical-align:middle}html body .task-list-item-checkbox:hover{cursor:pointer}html body blockquote{margin:16px 0;font-size:inherit;padding:0 15px;color:#5c5c5c;background-color:#f0f0f0;border-left:4px solid #d6d6d6}html body blockquote>:first-child{margin-top:0}html body blockquote>:last-child{margin-bottom:0}html body hr{height:4px;margin:32px 0;background-color:#d6d6d6;border:0 none}html body table{margin:10px 0 15px 0;border-collapse:collapse;border-spacing:0;display:block;width:100%;overflow:auto;word-break:normal;word-break:keep-all}html body table th{font-weight:700;color:#000}html body table td,html body table th{border:1px solid #d6d6d6;padding:6px 13px}html body dl{padding:0}html body dl dt{padding:0;margin-top:16px;font-size:1em;font-style:italic;font-weight:700}html body dl dd{padding:0 16px;margin-bottom:16px}html body code{font-family:Menlo,Monaco,Consolas,'Courier New',monospace;font-size:.85em;color:#000;background-color:#f0f0f0;border-radius:3px;padding:.2em 0}html body code::after,html body code::before{letter-spacing:-.2em;content:'\00a0'}html body pre>code{padding:0;margin:0;word-break:normal;white-space:pre;background:0 0;border:0}html body .highlight{margin-bottom:16px}html body .highlight pre,html body pre{padding:1em;overflow:auto;line-height:1.45;border:#d6d6d6;border-radius:3px}html body .highlight pre{margin-bottom:0;word-break:normal}html body pre code,html body pre tt{display:inline;max-width:initial;padding:0;margin:0;overflow:initial;line-height:inherit;word-wrap:normal;background-color:transparent;border:0}html body pre code:after,html body pre code:before,html body pre tt:after,html body pre tt:before{content:normal}html body blockquote,html body dl,html body ol,html body p,html body pre,html body ul{margin-top:0;margin-bottom:16px}html body kbd{color:#000;border:1px solid #d6d6d6;border-bottom:2px solid #c7c7c7;padding:2px 4px;background-color:#f0f0f0;border-radius:3px}@media print{html body{background-color:#fff}html body h1,html body h2,html body h3,html body h4,html body h5,html body h6{color:#000;page-break-after:avoid}html body blockquote{color:#5c5c5c}html body pre{page-break-inside:avoid}html body table{display:table}html body img{display:block;max-width:100%;max-height:100%}html body code,html body pre{word-wrap:break-word;white-space:pre}}.markdown-preview{width:100%;height:100%;box-sizing:border-box}.markdown-preview ul{list-style:disc}.markdown-preview ul ul{list-style:circle}.markdown-preview ul ul ul{list-style:square}.markdown-preview ol{list-style:decimal}.markdown-preview ol ol,.markdown-preview ul ol{list-style-type:lower-roman}.markdown-preview ol ol ol,.markdown-preview ol ul ol,.markdown-preview ul ol ol,.markdown-preview ul ul ol{list-style-type:lower-alpha}.markdown-preview .newpage,.markdown-preview .pagebreak{page-break-before:always}.markdown-preview pre.line-numbers{position:relative;padding-left:3.8em;counter-reset:linenumber}.markdown-preview pre.line-numbers>code{position:relative}.markdown-preview pre.line-numbers .line-numbers-rows{position:absolute;pointer-events:none;top:1em;font-size:100%;left:0;width:3em;letter-spacing:-1px;border-right:1px solid #999;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.markdown-preview pre.line-numbers .line-numbers-rows>span{pointer-events:none;display:block;counter-increment:linenumber}.markdown-preview pre.line-numbers .line-numbers-rows>span:before{content:counter(linenumber);color:#999;display:block;padding-right:.8em;text-align:right}.markdown-preview .mathjax-exps .MathJax_Display{text-align:center!important}.markdown-preview:not([data-for=preview]) .code-chunk .code-chunk-btn-group{display:none}.markdown-preview:not([data-for=preview]) .code-chunk .status{display:none}.markdown-preview:not([data-for=preview]) .code-chunk .output-div{margin-bottom:16px}.markdown-preview .md-toc{padding:0}.markdown-preview .md-toc .md-toc-link-wrapper .md-toc-link{display:inline;padding:.25rem 0}.markdown-preview .md-toc .md-toc-link-wrapper .md-toc-link div,.markdown-preview .md-toc .md-toc-link-wrapper .md-toc-link p{display:inline}.markdown-preview .md-toc .md-toc-link-wrapper.highlighted .md-toc-link{font-weight:800}.scrollbar-style::-webkit-scrollbar{width:8px}.scrollbar-style::-webkit-scrollbar-track{border-radius:10px;background-color:transparent}.scrollbar-style::-webkit-scrollbar-thumb{border-radius:5px;background-color:rgba(150,150,150,.66);border:4px solid rgba(150,150,150,.66);background-clip:content-box}html body[for=html-export]:not([data-presentation-mode]){position:relative;width:100%;height:100%;top:0;left:0;margin:0;padding:0;overflow:auto}html body[for=html-export]:not([data-presentation-mode]) .markdown-preview{position:relative;top:0;min-height:100vh}@media screen and (min-width:914px){html body[for=html-export]:not([data-presentation-mode]) .markdown-preview{padding:2em calc(50% - 457px + 2em)}}@media screen and (max-width:914px){html body[for=html-export]:not([data-presentation-mode]) .markdown-preview{padding:2em}}@media screen and (max-width:450px){html body[for=html-export]:not([data-presentation-mode]) .markdown-preview{font-size:14px!important;padding:1em}}@media print{html body[for=html-export]:not([data-presentation-mode]) #sidebar-toc-btn{display:none}}html body[for=html-export]:not([data-presentation-mode]) #sidebar-toc-btn{position:fixed;bottom:8px;left:8px;font-size:28px;cursor:pointer;color:inherit;z-index:99;width:32px;text-align:center;opacity:.4}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] #sidebar-toc-btn{opacity:1}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc{position:fixed;top:0;left:0;width:300px;height:100%;padding:32px 0 48px 0;font-size:14px;box-shadow:0 0 4px rgba(150,150,150,.33);box-sizing:border-box;overflow:auto;background-color:inherit}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar{width:8px}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar-track{border-radius:10px;background-color:transparent}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar-thumb{border-radius:5px;background-color:rgba(150,150,150,.66);border:4px solid rgba(150,150,150,.66);background-clip:content-box}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc a{text-decoration:none}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc{padding:0 16px}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper .md-toc-link{display:inline;padding:.25rem 0}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper .md-toc-link div,html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper .md-toc-link p{display:inline}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper.highlighted .md-toc-link{font-weight:800}html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{left:300px;width:calc(100% - 300px);padding:2em calc(50% - 457px - 300px / 2);margin:0;box-sizing:border-box}@media screen and (max-width:1274px){html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{padding:2em}}@media screen and (max-width:450px){html body[for=html-export]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{width:100%}}html body[for=html-export]:not([data-presentation-mode]):not([html-show-sidebar-toc]) .markdown-preview{left:50%;transform:translateX(-50%)}html body[for=html-export]:not([data-presentation-mode]):not([html-show-sidebar-toc]) .md-sidebar-toc{display:none}
/* Please visit the URL below for more information: */
/*   https://shd101wyy.github.io/markdown-preview-enhanced/#/customize-css */

      </style>
      <!-- The content below will be included at the end of the <head> element. --><script type="text/javascript">
  document.addEventListener("DOMContentLoaded", function () {
    // your code here
  });
</script></head><body for="html-export">
    
    
      <div class="crossnote markdown-preview  ">
      
<h1 id="python入门课程">python入门课程 </h1>
<h2 id="网络">网络 </h2>
<p>guest402 qazxswedc</p>
<h2 id="目标">目标 </h2>
<ul>
<li>理解基本的编程概念。</li>
<li>学习Python的基础语法。</li>
<li>使用Python解决简单的数学问题。</li>
</ul>
<h2 id="内容">内容 </h2>
<h3 id="1-介绍编程和python">1. 介绍编程和Python </h3>
<h4 id="计算机如何工作">计算机如何工作 </h4>
<p>计算机是一种能够接收输入数据，处理这些数据，然后产生输出的电子设备。其核心原理基于冯·诺依曼架构，这是一种存储程序计算机的概念，其中程序和数据都存储在相同的存储器中。计算机的主要组成部分包括：</p>
<ol>
<li>
<p><strong>中央处理器 (CPU)</strong>：这是计算机的大脑，负责执行指令集中的所有计算任务。CPU 从内存中读取指令，解码并执行它们。</p>
</li>
<li>
<p><strong>随机存取存储器 (RAM)</strong>：这是一种易失性存储器，当计算机关闭时，RAM 中的数据会丢失。RAM 用于存储正在运行的程序和数据。</p>
</li>
<li>
<p><strong>只读存储器 (ROM)</strong>：这是一种非易失性存储器，即使在断电的情况下也能保存数据。ROM 通常用于存储启动代码和其他关键系统信息。</p>
</li>
<li>
<p><strong>输入/输出设备 (I/O)</strong>：如键盘、鼠标、显示器和打印机，用于用户与计算机之间的交互。</p>
</li>
<li>
<p><strong>存储设备</strong>：如硬盘驱动器 (HDD)、固态硬盘 (SSD) 和闪存驱动器，用于长期存储数据。</p>
</li>
<li>
<p><strong>总线系统</strong>：这是计算机内部通信的通道，允许数据在各个部件之间传输。</p>
</li>
</ol>
<p>计算机的工作流程大致如下：</p>
<ul>
<li>输入设备接收用户或外部来源的数据。</li>
<li>数据被送入内存，并由 CPU 进行处理。</li>
<li>处理后的数据被送回内存或输出设备，如显示器或打印机。</li>
<li>当需要时，数据也可以被保存到持久性存储设备上。</li>
</ul>
<h4 id="编程语言介绍">编程语言介绍 </h4>
<p>编程语言是用于向计算机发出指令的一种形式化语言。它们是人与计算机沟通的媒介，使得程序员能够以结构化和逻辑化的方式描述计算机应该执行的任务。以下是关于编程语言的一些关键点：</p>
<h5 id="分类">分类 </h5>
<p>编程语言可以按照不同的标准分类，以下是几种常见的分类方式：</p>
<ol>
<li>
<p><strong>按抽象程度分类</strong>：</p>
<ul>
<li><strong>机器语言</strong>：由二进制数字（0和1）组成，是计算机硬件能直接识别的语言，但对人类来说难以阅读和编写。</li>
<li><strong>汇编语言</strong>：使用助记符代替机器语言中的二进制指令，相对于机器语言更易读，但仍然与特定的计算机硬件紧密相关。</li>
<li><strong>高级语言</strong>：如 Python、Java、C++ 等，它们接近自然语言，具有抽象数据类型和控制结构，易于编写和理解，且与硬件相对独立。</li>
</ul>
</li>
<li>
<p><strong>按执行方式分类</strong>：</p>
<ul>
<li><strong>编译型语言</strong>：如 C、C++，源代码首先被编译成机器语言，然后在目标机器上运行。</li>
<li><strong>解释型语言</strong>：如 Python、Ruby，代码在运行时由解释器逐行解释执行，无需预先编译。</li>
<li><strong>半编译半解释型语言</strong>：如 Java，代码先被编译成字节码，再由虚拟机解释执行。</li>
</ul>
</li>
<li>
<p><strong>按用途分类</strong>：</p>
<ul>
<li><strong>通用型语言</strong>：如 Python 和 Java，适用于广泛的编程领域。</li>
<li><strong>专用型语言</strong>：如 SQL（用于数据库查询）、HTML（用于网页结构）和 MATLAB（用于数值计算）。</li>
</ul>
</li>
</ol>
<h5 id="作用">作用 </h5>
<p>编程语言的作用主要体现在以下几个方面：</p>
<ul>
<li><strong>表达意图</strong>：编程语言使程序员能够清晰地表达算法和数据结构，从而解决具体问题。</li>
<li><strong>控制硬件</strong>：尽管大多数现代编程语言都远离了底层硬件细节，但它们仍提供了足够的控制来优化性能和资源使用。</li>
<li><strong>促进合作</strong>：良好的编程语言设计和标准的编程实践有助于团队协作，使多人共同开发和维护软件成为可能。</li>
<li><strong>可移植性</strong>：一些语言如 Java 和 Python 具有很好的跨平台能力，可以在不同的操作系统和硬件上运行相同的代码。</li>
<li><strong>可扩展性</strong>：通过库和框架，编程语言支持功能的快速扩展和复用，降低了开发新应用的成本和时间。</li>
</ul>
<p>总之，编程语言是计算机科学和软件工程的重要组成部分，它们不仅反映了计算机硬件的演变，也体现了人类在软件开发领域的智慧和创新。</p>
<h4 id="python介绍">python介绍 </h4>
<p>Python 是一种高级、通用的编程语言。它的设计理念强调代码的可读性和简洁的语法，这使得 Python 成为初学者学习编程的理想选择，同时也被专业开发者广泛用于各种复杂的项目中。Python 支持多种编程范式，包括面向对象、命令式、函数式和过程式编程风格。<br>
Python 语言的设计哲学是“优雅”、“明确”、“简单”。它的语法结构清晰，易于阅读，有助于减少程序维护成本。Python 支持模块化和包，可以轻松地将代码组织成模块或包，以便于重用和管理。</p>
<h3 id="2-python环境设置">2. Python环境设置 </h3>
<h4 id="下载-python">下载 Python </h4>
<p>要开始使用 Python，你需要从官方网站下载并安装 Python 解释器。官方提供多个版本的 Python，但目前最推荐的是 Python 3.x 版本。</p>
<p><strong>Python 官方下载地址：</strong></p>
<ul>
<li><a href="https://www.python.org/downloads/">https://www.python.org/downloads/</a></li>
</ul>
<p>在下载页面上，你可以根据你的操作系统（Windows、macOS 或 Linux）选择合适的安装包进行下载。对于 Windows 和 macOS 用户，通常会有图形界面的安装程序；而对于 Linux 用户，则可能需要通过命令行使用包管理工具来安装。</p>
<h4 id="pycharm-社区版">PyCharm 社区版 </h4>
<p>PyCharm 是一个非常流行的 Python 集成开发环境 (IDE)，它提供了丰富的功能，如代码编辑、调试、测试、版本控制等，非常适合开发大型项目。PyCharm 有两个版本：专业版和社区版。社区版是免费且开源的，而专业版则包含更多高级功能，但需要付费购买。</p>
<p><strong>PyCharm 社区版下载地址：</strong></p>
<ul>
<li><a href="https://www.jetbrains.com/pycharm/download/#section=windows">https://www.jetbrains.com/pycharm/download/#section=windows</a></li>
</ul>
<p>访问上述链接后，选择适合你操作系统的 PyCharm 社区版进行下载。PyCharm 提供了针对 Windows、macOS 和 Linux 的版本，确保选择正确的版本以避免兼容性问题。</p>
<h4 id="安装与配置">安装与配置 </h4>
<ol>
<li>
<p><strong>安装 Python</strong>：</p>
<ul>
<li>下载完成后，运行安装程序。</li>
<li>在安装过程中，务必勾选 "Add Python to PATH" 选项，这样可以在任何地方直接运行 Python 命令。</li>
<li>点击 "Install Now" 开始安装。</li>
</ul>
</li>
<li>
<p><strong>安装 PyCharm</strong>：</p>
<ul>
<li>同样地，下载完成后运行安装程序。</li>
<li>跟随安装向导完成安装过程，可以选择自定义安装路径和组件。</li>
</ul>
</li>
<li>
<p><strong>验证安装</strong>：</p>
<ul>
<li>打开命令行工具（Windows 的命令提示符或 PowerShell，macOS 的 Terminal，Linux 的终端）。</li>
<li>输入 <code>python --version</code> ，检查 Python 是否正确安装。</li>
</ul>
</li>
</ol>
<p>完成以上步骤后，你就有了一个基本的 Python 开发环境，可以开始编写你的第一个 Python 程序了！</p>
<h4 id="运行第一个python程序hello-world">运行第一个Python程序：“Hello, World!” </h4>
<h3 id="3-基础语法">3. 基础语法 </h3>
<p>在这一章节，我们将探索 Python 的一些基础语法元素，包括变量、数据类型以及基本的输入输出操作。</p>
<h3 id="变量和数据类型">变量和数据类型 </h3>
<p>在 Python 中，变量是一个用于存储数据的标识符。你可以给变量赋值，之后在程序中引用这个值。Python 是一种动态类型语言，这意味着你不需要显式声明变量的类型，类型是由赋给变量的值决定的。</p>
<p><strong>常见数据类型</strong>：</p>
<ul>
<li><strong>整数 (<code>int</code>)</strong>：如 <code>42</code>。</li>
<li><strong>浮点数 (<code>float</code>)</strong>：如 <code>3.14</code>。</li>
<li><strong>字符串 (<code>str</code>)</strong>：如 <code>"Hello, world!"</code>。</li>
</ul>
<h3 id="输入输出">输入输出 </h3>
<p>Python 提供了一些内置函数来处理输入和输出：</p>
<ul>
<li><strong><code>print()</code></strong>：用于在屏幕上显示文本或变量的值。</li>
<li><strong><code>input()</code></strong>：用于接收用户的键盘输入。</li>
</ul>
<h3 id="简单的算术运算符">简单的算术运算符 </h3>
<p>Python 支持基本的算术运算：</p>
<ul>
<li>加法 <code>+</code></li>
<li>减法 <code>-</code></li>
<li>乘法 <code>*</code></li>
<li>浮点除法 <code>/</code></li>
<li>整数除法 <code>//</code></li>
<li>取模 <code>%</code></li>
<li>幂运算 <code>**</code></li>
</ul>
<h4 id="练习题">练习题 </h4>
<p><strong>Q:</strong> 创建一个变量<code>a</code>赋值为10，然后创建一个变量<code>b</code>赋值为20，最后打印<code>a + b</code>的结果。</p>
<h3 id="3x基础语法扩展">3x：基础语法（扩展） </h3>
<h3 id="变量和数据类型-1">变量和数据类型 </h3>
<p>除了我们之前提到的数字和字符串，Python 还提供了以下数据类型：</p>
<ul>
<li><strong>列表 (<code>list</code>)</strong>：有序的元素集合，可以包含不同类型的元素，元素可以通过索引访问。</li>
<li><strong>集合 (<code>set</code>)</strong>：无序且不重复的元素集合，常用于去除重复项或进行集合运算（如交集、并集）。</li>
<li><strong>字典 (<code>dict</code>)</strong>：键值对的集合，键必须是不可变类型，值可以是任意类型。</li>
<li><strong>范围 (<code>range</code>)</strong>：生成一系列连续的整数，常用于循环控制。</li>
</ul>
<h3 id="列表-list">列表 (<code>list</code>) </h3>
<p>列表是 Python 中非常灵活的数据类型，可以存储任何类型的数据，并且可以动态添加或删除元素。</p>
<p><strong>创建列表</strong>：</p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code>my_list <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token string">'four'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
</code></pre><p><strong>访问元素</strong>：</p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code>first_element <span class="token operator">=</span> my_list<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>  <span class="token comment"># 返回 1</span>
last_element <span class="token operator">=</span> my_list<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>  <span class="token comment"># 返回 5</span>
</code></pre><p><strong>修改元素</strong>：</p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code>my_list<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'three'</span>
</code></pre><h3 id="集合-set">集合 (<code>set</code>) </h3>
<p>集合是一个无序且不包含重复元素的数据结构。</p>
<p><strong>创建集合</strong>：</p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code>my_set <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">}</span>  <span class="token comment"># 结果为 {1, 2, 3}</span>
</code></pre><p><strong>集合操作</strong>：</p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code>another_set <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">}</span>
union_set <span class="token operator">=</span> my_set<span class="token punctuation">.</span>union<span class="token punctuation">(</span>another_set<span class="token punctuation">)</span>  <span class="token comment"># 或者使用 | 操作符</span>
intersection_set <span class="token operator">=</span> my_set<span class="token punctuation">.</span>intersection<span class="token punctuation">(</span>another_set<span class="token punctuation">)</span>  <span class="token comment"># 或者使用 &amp; 操作符</span>
</code></pre><h3 id="字典-dict">字典 (<code>dict</code>) </h3>
<p>字典是一种映射类型，由键值对组成，键用于查找对应的值。</p>
<p><strong>创建字典</strong>：</p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code>my_dict <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">'name'</span><span class="token punctuation">:</span> <span class="token string">'John'</span><span class="token punctuation">,</span> <span class="token string">'age'</span><span class="token punctuation">:</span> <span class="token number">30</span><span class="token punctuation">}</span>
</code></pre><p><strong>访问字典元素</strong>：</p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code>name <span class="token operator">=</span> my_dict<span class="token punctuation">[</span><span class="token string">'name'</span><span class="token punctuation">]</span>  <span class="token comment"># 返回 'John'</span>
</code></pre><p><strong>修改字典</strong>：</p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code>my_dict<span class="token punctuation">[</span><span class="token string">'age'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">31</span>
</code></pre><h3 id="范围-range">范围 (<code>range</code>) </h3>
<p><code>range()</code> 函数用于生成一个整数序列，通常用于循环中。</p>
<p><strong>使用范围</strong>：</p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token keyword keyword-for">for</span> i <span class="token keyword keyword-in">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span>  <span class="token comment"># 输出 0 到 4</span>
</code></pre><p>现在你已经了解了 Python 中更全面的基础数据类型。这些数据类型将帮助你更有效地组织和处理数据。在接下来的章节中，我们将继续深入学习 Python 的其他高级特性。</p>
<h3 id="4-控制结构">4. 控制结构 </h3>
<p>控制结构允许你根据条件改变程序的执行流程。在 Python 中，有两种主要的控制结构：条件语句和循环语句。</p>
<h3 id="条件语句">条件语句 </h3>
<p>条件语句（<code>if</code>, <code>elif</code>, <code>else</code>）用于基于条件的真假来决定程序的分支执行路径。</p>
<p><strong>语法</strong>：</p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token keyword keyword-if">if</span> condition<span class="token punctuation">:</span>
    <span class="token comment"># 如果 condition 为真，则执行这里的代码块</span>
<span class="token keyword keyword-elif">elif</span> another_condition<span class="token punctuation">:</span>
    <span class="token comment"># 如果前面的条件都不满足，但 this_condition 为真，则执行这里的代码块</span>
<span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>
    <span class="token comment"># 如果所有的 if 和 elif 条件都不满足，则执行这里的代码块</span>
</code></pre><h3 id="循环">循环 </h3>
<p>循环语句允许你重复执行一段代码直到满足特定条件。</p>
<p><strong><code>for</code> 循环</strong>：</p>
<p>用于遍历序列（如列表、元组、字符串）或其他可迭代对象。</p>
<p><strong>语法</strong>：</p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token keyword keyword-for">for</span> variable <span class="token keyword keyword-in">in</span> iterable<span class="token punctuation">:</span>
    <span class="token comment"># 执行的代码块</span>
</code></pre><p><strong><code>while</code> 循环</strong>：</p>
<p>只要条件为真，就会重复执行一段代码。</p>
<p><strong>语法</strong>：</p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token keyword keyword-while">while</span> condition<span class="token punctuation">:</span>
    <span class="token comment"># 执行的代码块</span>
</code></pre><h3 id="条件语句示例">条件语句示例 </h3>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token comment"># 示例：判断一个数是正数、负数还是零</span>
number <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">"请输入一个数字: "</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment"># 使用模运算符 % 判断数的奇偶性</span>
<span class="token keyword keyword-if">if</span> number <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>number<span class="token punctuation">,</span> <span class="token string">"是一个偶数"</span><span class="token punctuation">)</span>
<span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>
    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>number<span class="token punctuation">,</span> <span class="token string">"是一个奇数"</span><span class="token punctuation">)</span>
</code></pre><h3 id="循环语句示例">循环语句示例 </h3>
<h4 id="for-循环示例"><code>for</code> 循环示例 </h4>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token comment"># 示例：遍历列表中的元素并打印</span>
fruits <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'apple'</span><span class="token punctuation">,</span> <span class="token string">'banana'</span><span class="token punctuation">,</span> <span class="token string">'cherry'</span><span class="token punctuation">]</span>

<span class="token keyword keyword-for">for</span> fruit <span class="token keyword keyword-in">in</span> fruits<span class="token punctuation">:</span>
    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>fruit<span class="token punctuation">)</span>

<span class="token comment"># 使用 range() 函数</span>
<span class="token keyword keyword-for">for</span> i <span class="token keyword keyword-in">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">:</span>  <span class="token comment"># 从1到4</span>
    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"Counting:"</span><span class="token punctuation">,</span> i<span class="token punctuation">)</span>
</code></pre><h4 id="while-循环示例"><code>while</code> 循环示例 </h4>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token comment"># 示例：计算从 1 加到 10 的和</span>
total <span class="token operator">=</span> <span class="token number">0</span>
counter <span class="token operator">=</span> <span class="token number">1</span>

<span class="token keyword keyword-while">while</span> counter <span class="token operator">&lt;=</span> <span class="token number">10</span><span class="token punctuation">:</span>
    total <span class="token operator">+=</span> counter
    counter <span class="token operator">+=</span> <span class="token number">1</span>

<span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"The sum from 1 to 10 is:"</span><span class="token punctuation">,</span> total<span class="token punctuation">)</span>
</code></pre><h3 id="控制流语句示例">控制流语句示例 </h3>
<h4 id="break-语句"><code>break</code> 语句 </h4>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token comment"># 示例：在找到特定元素时提前结束循环</span>
search_for <span class="token operator">=</span> <span class="token string">'banana'</span>
fruits <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'apple'</span><span class="token punctuation">,</span> <span class="token string">'orange'</span><span class="token punctuation">,</span> <span class="token string">'banana'</span><span class="token punctuation">,</span> <span class="token string">'grape'</span><span class="token punctuation">]</span>

<span class="token keyword keyword-for">for</span> fruit <span class="token keyword keyword-in">in</span> fruits<span class="token punctuation">:</span>
    <span class="token keyword keyword-if">if</span> fruit <span class="token operator">==</span> search_for<span class="token punctuation">:</span>
        <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"Found"</span><span class="token punctuation">,</span> search_for<span class="token punctuation">)</span>
        <span class="token keyword keyword-break">break</span>
    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"Looking for"</span><span class="token punctuation">,</span> search_for<span class="token punctuation">)</span>
</code></pre><h4 id="continue-语句"><code>continue</code> 语句 </h4>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token comment"># 示例：跳过特定元素，继续循环</span>
fruits <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'apple'</span><span class="token punctuation">,</span> <span class="token string">'banana'</span><span class="token punctuation">,</span> <span class="token string">'cherry'</span><span class="token punctuation">]</span>

<span class="token keyword keyword-for">for</span> fruit <span class="token keyword keyword-in">in</span> fruits<span class="token punctuation">:</span>
    <span class="token keyword keyword-if">if</span> fruit <span class="token operator">==</span> <span class="token string">'banana'</span><span class="token punctuation">:</span>
        <span class="token keyword keyword-continue">continue</span>
    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"Eating"</span><span class="token punctuation">,</span> fruit<span class="token punctuation">)</span>
</code></pre><h4 id="pass-语句"><code>pass</code> 语句 </h4>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token comment"># 示例：当条件不满足时，什么也不做</span>
age <span class="token operator">=</span> <span class="token number">17</span>

<span class="token keyword keyword-if">if</span> age <span class="token operator">&gt;=</span> <span class="token number">18</span><span class="token punctuation">:</span>
    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"You are eligible to vote."</span><span class="token punctuation">)</span>
<span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>
    <span class="token keyword keyword-pass">pass</span>  <span class="token comment"># 这里可以放置后续代码，或者仅仅是占位符</span>
</code></pre><p>这些示例涵盖了 Python 中常用的控制结构。你可以尝试运行这些代码，看看它们是如何工作的。这对于理解程序的流程控制至关重要。如果你有任何疑问或需要进一步的解释，请随时提问。</p>
<h3 id="练习题-1">练习题 </h3>
<p><strong>Q:</strong> 编写一个程序，如果输入的数字大于10，则输出"大于10"，否则输出"不大于10"。</p>
<p><strong>Q:</strong> 编写一个程序，运用循环控制语句输出乘法表。</p>
<pre data-role="codeBlock" data-info="shell" class="language-shell shell"><code><span class="token assign-left variable">1x1</span><span class="token operator">=</span><span class="token number">1</span>	
<span class="token assign-left variable">1x2</span><span class="token operator">=</span><span class="token number">2</span>	<span class="token assign-left variable">2x2</span><span class="token operator">=</span><span class="token number">4</span>	
<span class="token assign-left variable">1x3</span><span class="token operator">=</span><span class="token number">3</span>	<span class="token assign-left variable">2x3</span><span class="token operator">=</span><span class="token number">6</span>	<span class="token assign-left variable">3x3</span><span class="token operator">=</span><span class="token number">9</span>	
<span class="token assign-left variable">1x4</span><span class="token operator">=</span><span class="token number">4</span>	<span class="token assign-left variable">2x4</span><span class="token operator">=</span><span class="token number">8</span>	<span class="token assign-left variable">3x4</span><span class="token operator">=</span><span class="token number">12</span>	<span class="token assign-left variable">4x4</span><span class="token operator">=</span><span class="token number">16</span>	
<span class="token assign-left variable">1x5</span><span class="token operator">=</span><span class="token number">5</span>	<span class="token assign-left variable">2x5</span><span class="token operator">=</span><span class="token number">10</span>	<span class="token assign-left variable">3x5</span><span class="token operator">=</span><span class="token number">15</span>	<span class="token assign-left variable">4x5</span><span class="token operator">=</span><span class="token number">20</span>	<span class="token assign-left variable">5x5</span><span class="token operator">=</span><span class="token number">25</span>	
<span class="token assign-left variable">1x6</span><span class="token operator">=</span><span class="token number">6</span>	<span class="token assign-left variable">2x6</span><span class="token operator">=</span><span class="token number">12</span>	<span class="token assign-left variable">3x6</span><span class="token operator">=</span><span class="token number">18</span>	<span class="token assign-left variable">4x6</span><span class="token operator">=</span><span class="token number">24</span>	<span class="token assign-left variable">5x6</span><span class="token operator">=</span><span class="token number">30</span>	<span class="token assign-left variable">6x6</span><span class="token operator">=</span><span class="token number">36</span>	
<span class="token assign-left variable">1x7</span><span class="token operator">=</span><span class="token number">7</span>	<span class="token assign-left variable">2x7</span><span class="token operator">=</span><span class="token number">14</span>	<span class="token assign-left variable">3x7</span><span class="token operator">=</span><span class="token number">21</span>	<span class="token assign-left variable">4x7</span><span class="token operator">=</span><span class="token number">28</span>	<span class="token assign-left variable">5x7</span><span class="token operator">=</span><span class="token number">35</span>	<span class="token assign-left variable">6x7</span><span class="token operator">=</span><span class="token number">42</span>	<span class="token assign-left variable">7x7</span><span class="token operator">=</span><span class="token number">49</span>	
<span class="token assign-left variable">1x8</span><span class="token operator">=</span><span class="token number">8</span>	<span class="token assign-left variable">2x8</span><span class="token operator">=</span><span class="token number">16</span>	<span class="token assign-left variable">3x8</span><span class="token operator">=</span><span class="token number">24</span>	<span class="token assign-left variable">4x8</span><span class="token operator">=</span><span class="token number">32</span>	<span class="token assign-left variable">5x8</span><span class="token operator">=</span><span class="token number">40</span>	<span class="token assign-left variable">6x8</span><span class="token operator">=</span><span class="token number">48</span>	<span class="token assign-left variable">7x8</span><span class="token operator">=</span><span class="token number">56</span>	<span class="token assign-left variable">8x8</span><span class="token operator">=</span><span class="token number">64</span>	
<span class="token assign-left variable">1x9</span><span class="token operator">=</span><span class="token number">9</span>	<span class="token assign-left variable">2x9</span><span class="token operator">=</span><span class="token number">18</span>	<span class="token assign-left variable">3x9</span><span class="token operator">=</span><span class="token number">27</span>	<span class="token assign-left variable">4x9</span><span class="token operator">=</span><span class="token number">36</span>	<span class="token assign-left variable">5x9</span><span class="token operator">=</span><span class="token number">45</span>	<span class="token assign-left variable">6x9</span><span class="token operator">=</span><span class="token number">54</span>	<span class="token assign-left variable">7x9</span><span class="token operator">=</span><span class="token number">63</span>	<span class="token assign-left variable">8x9</span><span class="token operator">=</span><span class="token number">72</span>	<span class="token assign-left variable">9x9</span><span class="token operator">=</span><span class="token number">81</span>	
</code></pre><h3 id="5-函数和模块">5. 函数和模块 </h3>
<p>在Python中，函数和模块是组织代码和实现功能重用的关键概念。下面我会详细解释这两个概念，并给出相应的示例代码。</p>
<h4 id="函数定义和调用">函数定义和调用 </h4>
<p>在Python中，函数使用<code>def</code>关键字定义。函数可以接受任意数量的参数，执行特定任务，并且可能返回一个值。函数的基本结构如下：</p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token keyword keyword-def">def</span> <span class="token function">function_name</span><span class="token punctuation">(</span>parameters<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># 函数体</span>
    <span class="token keyword keyword-return">return</span> value
</code></pre><p>其中<code>function_name</code>是你给函数起的名字，<code>parameters</code>是函数的输入，而<code>return value</code>是函数的输出。如果函数没有<code>return</code>语句，则默认返回<code>None</code>。</p>
<p><strong>示例代码：</strong></p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token keyword keyword-def">def</span> <span class="token function">multiply</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">""" 返回两个数的乘积 """</span>
    <span class="token keyword keyword-return">return</span> a <span class="token operator">*</span> b

<span class="token comment"># 调用函数</span>
result <span class="token operator">=</span> multiply<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span>
<span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>  <span class="token comment"># 输出：12</span>
</code></pre><h4 id="导入模块">导入模块 </h4>
<p>模块是包含Python代码的文件，它们可以被其他程序导入并使用。Python有一个庞大的标准库，包含了各种各样的模块，如<code>math</code>模块，用于数学计算。</p>
<p>要使用模块中的函数或变量，需要先导入该模块。可以使用<code>import</code>语句来导入整个模块，或者只导入模块中的特定函数或变量。</p>
<p><strong>示例代码：</strong></p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token keyword keyword-import">import</span> math

<span class="token comment"># 使用math模块中的pow函数计算2的3次方</span>
power_result <span class="token operator">=</span> math<span class="token punctuation">.</span><span class="token builtin">pow</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span>
<span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>power_result<span class="token punctuation">)</span>  <span class="token comment"># 输出：8.0</span>

<span class="token comment"># 你也可以使用从模块中直接导入的方式来调用pow函数</span>
<span class="token keyword keyword-from">from</span> math <span class="token keyword keyword-import">import</span> <span class="token builtin">pow</span>

<span class="token comment"># 直接调用pow函数计算3的2次方</span>
power_result <span class="token operator">=</span> <span class="token builtin">pow</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span>
<span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>power_result<span class="token punctuation">)</span>  <span class="token comment"># 输出：9.0</span>
</code></pre><p>这些示例展示了如何定义和调用函数，以及如何导入和使用Python中的模块。通过这种方式，你可以创建可重用的代码块，并利用Python标准库的强大功能。</p>
<h4 id="练习题-2">练习题 </h4>
<p><strong>Q:</strong> 使用<code>math</code>模块计算圆的面积，半径由用户输入。</p>
<h3 id="6-用python解方程和不等式">6. 用Python解方程和不等式 </h3>
<p>在Python中，<code>sympy</code>是一个强大的库，用于符号数学。它能够处理各种代数方程，包括线性方程、多项式方程、微分方程等。下面我将介绍如何使用<code>sympy</code>模块来解一元二次方程。</p>
<h4 id="使用sympy模块解代数方程">使用<code>sympy</code>模块解代数方程 </h4>
<p><code>sympy</code>模块允许你定义符号变量，然后使用这些变量来构造和解方程。为了解一元二次方程<code>ax^2 + bx + c = 0</code>，我们需要首先导入<code>sympy</code>模块，定义变量<code>x</code>，然后构造方程并求解。</p>
<h4 id="实践解一元二次方程">实践：解一元二次方程 </h4>
<p>假设我们有方程<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>2</mn><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo><mn>3</mn><mi>x</mi><mo>−</mo><mn>2</mn><mo>=</mo><mn>0</mn></mrow><annotation encoding="application/x-tex">2x^2 + 3x - 2 = 0</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8974em;vertical-align:-0.0833em;"></span><span class="mord">2</span><span class="mord"><span class="mord mathnormal">x</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">3</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">2</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">0</span></span></span></span>，我们将使用<code>sympy</code>来解这个方程。</p>
<p><strong>示例代码：</strong></p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token keyword keyword-from">from</span> sympy <span class="token keyword keyword-import">import</span> symbols<span class="token punctuation">,</span> Eq<span class="token punctuation">,</span> solve

<span class="token comment"># 定义符号变量</span>
x <span class="token operator">=</span> symbols<span class="token punctuation">(</span><span class="token string">'x'</span><span class="token punctuation">)</span>

<span class="token comment"># 定义方程</span>
equation <span class="token operator">=</span> Eq<span class="token punctuation">(</span><span class="token number">2</span><span class="token operator">*</span>x<span class="token operator">**</span><span class="token number">2</span> <span class="token operator">+</span> <span class="token number">3</span><span class="token operator">*</span>x <span class="token operator">-</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>

<span class="token comment"># 解方程</span>
solutions <span class="token operator">=</span> solve<span class="token punctuation">(</span>equation<span class="token punctuation">,</span> x<span class="token punctuation">)</span>

<span class="token comment"># 输出解</span>
<span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"方程的解是："</span><span class="token punctuation">)</span>
<span class="token keyword keyword-for">for</span> sol <span class="token keyword keyword-in">in</span> solutions<span class="token punctuation">:</span>
    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>sol<span class="token punctuation">)</span>
</code></pre><p><strong>运行结果：</strong></p>
<p>当你运行这段代码时，它将输出方程<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>2</mn><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo><mn>3</mn><mi>x</mi><mo>−</mo><mn>2</mn><mo>=</mo><mn>0</mn></mrow><annotation encoding="application/x-tex">2x^2 + 3x - 2 = 0</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8974em;vertical-align:-0.0833em;"></span><span class="mord">2</span><span class="mord"><span class="mord mathnormal">x</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">3</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">2</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">0</span></span></span></span>的所有解。</p>
<h4 id="解析">解析： </h4>
<p>在这个示例中，我们首先从<code>sympy</code>模块导入了<code>symbols</code>，<code>Eq</code>和<code>solve</code>函数。<code>symbols</code>函数用于创建符号变量，<code>Eq</code>用于创建方程，而<code>solve</code>函数用于求解方程。</p>
<ul>
<li>我们使用<code>symbols('x')</code>定义了符号变量<code>x</code>。</li>
<li>接着，我们使用<code>Eq(2*x**2 + 3*x - 2, 0)</code>定义了一元二次方程。</li>
<li>最后，我们使用<code>solve(equation, x)</code>求解方程，并将解存储在<code>solutions</code>变量中。</li>
</ul>
<p><code>sympy</code>不仅可以解一元二次方程，还可以解更高阶的多项式方程，甚至是微分方程和积分问题。它是一个非常强大且灵活的数学库，适用于各种数学问题的求解。</p>
<p>当然，<code>sympy</code>模块同样擅长解方程组和不等式。下面我将分别演示如何使用<code>sympy</code>来解一个线性方程组和一个不等式。</p>
<h3 id="解方程组">解方程组 </h3>
<p>假设我们有以下方程组：</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mo fence="true">{</mo><mtable rowspacing="0.36em" columnalign="left left" columnspacing="1em"><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><mrow><mi>x</mi><mo>+</mo><mi>y</mi></mrow></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><mrow><mo>=</mo><mn>1</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><mrow><mn>2</mn><mi>x</mi><mo>−</mo><mi>y</mi></mrow></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="false"><mrow><mo>=</mo><mn>3</mn></mrow></mstyle></mtd></mtr></mtable></mrow><annotation encoding="application/x-tex">\begin{cases}
x + y &amp;= 1 \\
2x - y &amp;= 3
\end{cases}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:3em;vertical-align:-1.25em;"></span><span class="minner"><span class="mopen delimcenter" style="top:0em;"><span class="delimsizing size4">{</span></span><span class="mord"><span class="mtable"><span class="col-align-l"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.69em;"><span style="top:-3.69em;"><span class="pstrut" style="height:3.008em;"></span><span class="mord"><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span></span></span><span style="top:-2.25em;"><span class="pstrut" style="height:3.008em;"></span><span class="mord"><span class="mord">2</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:1.19em;"><span></span></span></span></span></span><span class="arraycolsep" style="width:1em;"></span><span class="col-align-l"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.69em;"><span style="top:-3.69em;"><span class="pstrut" style="height:3.008em;"></span><span class="mord"><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mord">1</span></span></span><span style="top:-2.25em;"><span class="pstrut" style="height:3.008em;"></span><span class="mord"><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mord">3</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:1.19em;"><span></span></span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span></span></span></span></span></p>
<p>我们将使用<code>sympy</code>来解这个方程组。</p>
<p><strong>示例代码：</strong></p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token keyword keyword-from">from</span> sympy <span class="token keyword keyword-import">import</span> symbols<span class="token punctuation">,</span> Eq<span class="token punctuation">,</span> solve

<span class="token comment"># 定义符号变量</span>
x<span class="token punctuation">,</span> y <span class="token operator">=</span> symbols<span class="token punctuation">(</span><span class="token string">'x y'</span><span class="token punctuation">)</span>

<span class="token comment"># 定义方程组</span>
eq1 <span class="token operator">=</span> Eq<span class="token punctuation">(</span>x <span class="token operator">+</span> y<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
eq2 <span class="token operator">=</span> Eq<span class="token punctuation">(</span><span class="token number">2</span><span class="token operator">*</span>x <span class="token operator">-</span> y<span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span>

<span class="token comment"># 解方程组</span>
solution <span class="token operator">=</span> solve<span class="token punctuation">(</span><span class="token punctuation">(</span>eq1<span class="token punctuation">,</span> eq2<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment"># 输出解</span>
<span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"方程组的解是："</span><span class="token punctuation">)</span>
<span class="token keyword keyword-for">for</span> var<span class="token punctuation">,</span> val <span class="token keyword keyword-in">in</span> solution<span class="token punctuation">.</span>items<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f"</span><span class="token interpolation"><span class="token punctuation">{</span>var<span class="token punctuation">}</span></span><span class="token string"> = </span><span class="token interpolation"><span class="token punctuation">{</span>val<span class="token punctuation">}</span></span><span class="token string">"</span></span><span class="token punctuation">)</span>
</code></pre><p><strong>运行结果：</strong></p>
<p>当你运行这段代码时，它将输出方程组的解。</p>
<h3 id="解不等式">解不等式 </h3>
<p><code>sympy</code>也可以解不等式，例如解不等式<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi><mo>−</mo><mn>2</mn><mo>&gt;</mo><mn>0</mn></mrow><annotation encoding="application/x-tex">x - 2 &gt; 0</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6667em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.6835em;vertical-align:-0.0391em;"></span><span class="mord">2</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">&gt;</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">0</span></span></span></span>。</p>
<p><strong>示例代码：</strong></p>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token keyword keyword-from">from</span> sympy <span class="token keyword keyword-import">import</span> symbols<span class="token punctuation">,</span> solve_univariate_inequality

<span class="token comment"># 定义符号变量</span>
x <span class="token operator">=</span> symbols<span class="token punctuation">(</span><span class="token string">'x'</span><span class="token punctuation">)</span>

<span class="token comment"># 定义不等式</span>
ineq <span class="token operator">=</span> x <span class="token operator">-</span> <span class="token number">2</span> <span class="token operator">&gt;</span> <span class="token number">0</span>

<span class="token comment"># 解不等式</span>
solution <span class="token operator">=</span> solve_univariate_inequality<span class="token punctuation">(</span>ineq<span class="token punctuation">,</span> x<span class="token punctuation">)</span>

<span class="token comment"># 输出解</span>
<span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"不等式的解集是："</span><span class="token punctuation">)</span>
<span class="token keyword keyword-print">print</span><span class="token punctuation">(</span>solution<span class="token punctuation">)</span>
</code></pre><p><strong>运行结果：</strong></p>
<p>当你运行这段代码时，它将输出不等式的解集。</p>
<h4 id="解析-1">解析： </h4>
<p>在解方程组的例子中，我们定义了两个符号变量<code>x</code>和<code>y</code>，然后构造了两个方程，并使用<code>solve()</code>函数求解了方程组。在解不等式的情况下，我们使用了<code>solve_univariate_inequality()</code>函数，它可以解一元不等式并返回解集。</p>
<p><code>sympy</code>的解不等式功能同样强大，可以处理包括线性不等式、多项式不等式在内的各种情况。</p>
<p>通过这些示例，你将能够掌握如何使用<code>sympy</code>来解方程、方程组和不等式，这对于数学和工程问题的求解非常有用。无论是解决学术问题还是实际应用中的数学难题，<code>sympy</code>都能提供强大的支持。</p>
<h3 id="展开代数表达式">展开代数表达式 </h3>
<p>展开一个代数表达式。表达式是 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>x</mi><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo><mo stretchy="false">(</mo><mi>x</mi><mo>−</mo><mn>1</mn><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">(x + 1)(x - 1)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">1</span><span class="mclose">)</span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">1</span><span class="mclose">)</span></span></span></span>。</p>
<h4 id="python代码示例">Python代码示例： </h4>
<pre data-role="codeBlock" data-info="python" class="language-python python"><code><span class="token keyword keyword-from">from</span> sympy <span class="token keyword keyword-import">import</span> symbols<span class="token punctuation">,</span> expand

<span class="token comment"># 定义符号变量x</span>
x <span class="token operator">=</span> symbols<span class="token punctuation">(</span><span class="token string">'x'</span><span class="token punctuation">)</span>

<span class="token comment"># 创建代数表达式(x + 1)(x - 1)</span>
expr <span class="token operator">=</span> <span class="token punctuation">(</span>x <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">*</span><span class="token punctuation">(</span>x <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span>

<span class="token comment"># 使用expand()函数展开表达式</span>
expanded_expr <span class="token operator">=</span> expand<span class="token punctuation">(</span>expr<span class="token punctuation">)</span>

<span class="token comment"># 打印原始表达式和展开后的表达式</span>
<span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"原始表达式:"</span><span class="token punctuation">,</span> expr<span class="token punctuation">)</span>
<span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">"展开后的表达式:"</span><span class="token punctuation">,</span> expanded_expr<span class="token punctuation">)</span>
</code></pre><h4 id="结果">结果： </h4>
<p>当你运行上面的代码时，你会看到：</p>
<pre data-role="codeBlock" data-info="" class="language-text"><code>原始表达式: (x + 1)*(x - 1)
展开后的表达式: x**2 - 1
</code></pre><h3 id="解析-2">解析： </h3>
<p>首先，我们需要告诉<code>sympy</code>我们正在处理的变量是什么。在这个例子中，变量是 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi></mrow><annotation encoding="application/x-tex">x</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">x</span></span></span></span>。</p>
<p>接下来，我们创建了我们的表达式 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>x</mi><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo><mo stretchy="false">(</mo><mi>x</mi><mo>−</mo><mn>1</mn><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">(x + 1)(x - 1)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">1</span><span class="mclose">)</span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">1</span><span class="mclose">)</span></span></span></span>。然后，我们使用<code>sympy</code>中的<code>expand()</code>函数来展开这个表达式，这意味着我们将按照分配律来相乘所有项。</p>
<p>最后，我们打印出原始表达式和展开后的表达式，以便比较。</p>
<p>这意味着 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>x</mi><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo><mo stretchy="false">(</mo><mi>x</mi><mo>−</mo><mn>1</mn><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">(x + 1)(x - 1)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">1</span><span class="mclose">)</span><span class="mopen">(</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">1</span><span class="mclose">)</span></span></span></span> 展开后变成了 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>x</mi><mn>2</mn></msup><mo>−</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">x^2 - 1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8974em;vertical-align:-0.0833em;"></span><span class="mord"><span class="mord mathnormal">x</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">1</span></span></span></span>。这实际上是一个代数恒等式，称为平方差公式，即 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>a</mi><mn>2</mn></msup><mo>−</mo><msup><mi>b</mi><mn>2</mn></msup><mo>=</mo><mo stretchy="false">(</mo><mi>a</mi><mo>+</mo><mi>b</mi><mo stretchy="false">)</mo><mo stretchy="false">(</mo><mi>a</mi><mo>−</mo><mi>b</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">a^2 - b^2 = (a + b)(a - b)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8974em;vertical-align:-0.0833em;"></span><span class="mord"><span class="mord mathnormal">a</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.8141em;"></span><span class="mord"><span class="mord mathnormal">b</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">a</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">b</span><span class="mclose">)</span><span class="mopen">(</span><span class="mord mathnormal">a</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">b</span><span class="mclose">)</span></span></span></span>。</p>
<h4 id="练习题-3">练习题 </h4>
<p><strong>Q:</strong> 使用<code>sympy</code>模块解方程<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>x</mi><mn>2</mn></msup><mo>−</mo><mn>5</mn><mi>x</mi><mo>+</mo><mn>6</mn><mo>=</mo><mn>0</mn></mrow><annotation encoding="application/x-tex">x^2 - 5x + 6 = 0</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8974em;vertical-align:-0.0833em;"></span><span class="mord"><span class="mord mathnormal">x</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">5</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">6</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">0</span></span></span></span>。<br>
<strong>Q:</strong> 使用<code>sympy</code>模块解方程组<br>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mo fence="true">{</mo><mtable rowspacing="0.36em" columnalign="left left" columnspacing="1em"><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><mrow><mn>3</mn><mi>x</mi><mo>+</mo><mn>2</mn><mi>y</mi><mo>=</mo><mn>7</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel="0" displaystyle="false"><mrow><mi>x</mi><mo>−</mo><mi>y</mi><mo>=</mo><mn>1</mn></mrow></mstyle></mtd></mtr></mtable></mrow><annotation encoding="application/x-tex">\begin{cases}
3x + 2y = 7 \\
x - y = 1
\end{cases}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:3em;vertical-align:-1.25em;"></span><span class="minner"><span class="mopen delimcenter" style="top:0em;"><span class="delimsizing size4">{</span></span><span class="mord"><span class="mtable"><span class="col-align-l"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.69em;"><span style="top:-3.69em;"><span class="pstrut" style="height:3.008em;"></span><span class="mord"><span class="mord">3</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mord">2</span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mord">7</span></span></span><span style="top:-2.25em;"><span class="pstrut" style="height:3.008em;"></span><span class="mord"><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mord">1</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:1.19em;"><span></span></span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span></span></span></span></span><br>
<strong>Q:</strong> 使用<code>sympy</code>模块解不等式</p>
<p><span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mn>2</mn><mi>x</mi><mo>−</mo><mn>3</mn><mo>&gt;</mo><mn>5</mn><mi>x</mi><mo>+</mo><mn>2</mn></mrow><annotation encoding="application/x-tex">2x - 3 &gt; 5x + 2</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">2</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.6835em;vertical-align:-0.0391em;"></span><span class="mord">3</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">&gt;</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">5</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">2</span></span></span></span></span></p>
<h3 id="综合练习题猜数字游戏">综合练习题：猜数字游戏 </h3>
<p><strong>Q:</strong> 编写一个猜数字游戏，程序随机生成一个1至10之间的整数，玩家需要通过提示来猜测这个数字。如果猜错了，告诉玩家是猜大了还是猜小了。</p>
<h3 id="综合练习题文字冒险游戏">综合练习题：文字冒险游戏 </h3>
<p><strong>Q：</strong></p>
<ul>
<li>设计一个至少包括三个地点的冒险游戏，玩家需要找到迷宫内的宝藏来获取胜利</li>
<li>玩家需要输入指令来控制角色行动。</li>
<li>你可以增加其他的功能，比如怪物，武器，魔法药水等。</li>
</ul>

      </div>
      
      
    
    
    
    
    
    
  
    </body></html>